package com.wind.pingpp.util;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.util.Log;

import com.pingplusplus.android.Pingpp;

/**
 * Created by wind on 2017/3/24.
 */
public class PaymentUtil {

    /**
     * 发起支付
     * @param fragment
     * @param charge
     */
    public static void startPay(Fragment fragment, String charge){
        Pingpp.createPayment(fragment,charge);
    }
    /**
     * 发起支付
     * @param activity
     * @param charge
     */
    public static void startPay(Activity activity, String charge){
        Pingpp.createPayment(activity, charge);
    }

    /**
     * 支付返回
     * @param requestCode
     * @param resultCode
     * @param data
     * @param callback
     */
    public static void onActivityResult(int requestCode, int resultCode, Intent data,PaymentCallback callback) {
        //支付页面返回处理
        if (requestCode == Pingpp.REQUEST_CODE_PAYMENT) {
            if (resultCode == Activity.RESULT_OK) {
                String result = data.getExtras().getString("pay_result");
                String errorMsg = data.getExtras().getString("error_msg"); // 错误信息
                String extraMsg = data.getExtras().getString("extra_msg"); // 错误信息
                // 处理返回值
                // "success" - 支付成功
                // "fail"    - 支付失败
                // "cancel"  - 取消支付
                // "invalid" - 支付插件未安装（一般是微信客户端未安装的情况）
                if ("success".equals(result)){
                   //callback.onPaySuccess();
                    callback.onPayReturn(PaymentState.SUCCESS,"支付成功");
                }else if ("cancel".equals(result)){
                   // showToast(activity,"已取消支付");
                    callback.onPayReturn(PaymentState.CANCEL,"已取消支付");
                    //ToastUtil.showToast(activity, "已取消支付");
                }else if ("fail".equals(result)){
                    //showToast(activity,"支付失败");
                    callback.onPayReturn(PaymentState.FAIL,"支付失败");
                    Log.e("PaymentUtil","errorMsg:"+errorMsg+"-extraMsg:"+extraMsg);
                    //ToastUtil.show(activity, "支付失败");
                }else if ("invalid".equals(result)){
                    callback.onPayReturn(PaymentState.INVALID,"客户端未安装");
                    //showToast(activity,"微信客户端未安装");
                    //ToastUtil.show(App.get(), "微信客户端未安装", ToastUtil.LENGTH_SHORT);
                }else {
                  /*  String errorMsg = data.getExtras().getString("error_msg"); // 错误信息
                    String extraMsg = data.getExtras().getString("extra_msg"); // 错误信息
                    showToast(activity,errorMsg);*/
                    callback.onPayReturn(PaymentState.UNKNOWN,errorMsg);
                }

            }
        }
    }

    public interface PaymentCallback{
        void onPayReturn(PaymentState state,String msg);
    }
    public enum PaymentState{
        SUCCESS,CANCEL,FAIL,INVALID,UNKNOWN
    }
}
